﻿unit UPPPanelTitle;

interface

uses
  SysUtils, Windows, Messages, Classes, Graphics, Controls, Forms,

  ExtCtrls, ShellAPI;

type
  TPPPanelTitle = class(TCustomControl)
  private
    FMainFrm: TCustomForm;
    FSideWidth: ShortInt;
    FMainPicture: TPicture;
    FBackgroundPic: TPicture;
    procedure SetSideWidth(const Value: ShortInt);
    procedure SetMainPicture(const Value: TPicture);
    procedure SetBackgroundPic(const Value: TPicture);

  protected
    // procedure WMNchist(var Msg: TMessage);message WM_NCHITTEST;
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
      X, Y: Integer); override;
    procedure DblClick; override;
    //
    procedure Paint; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

    function GetTitleRect: TRect;//
  published
    property Align default alTop;
    property Anchors;
    property BevelEdges;
    property BevelInner;
    property BevelKind;
    property BevelOuter;
    property BevelWidth;
    property BiDiMode;
    property BorderWidth;
    property Caption;
    property Color;
    property Constraints;
    property Ctl3D;
    property UseDockManager default True;
    property DockSite;
    property DoubleBuffered;
    property DragCursor;
    property DragKind;
    property DragMode;
    property Enabled;
    property Font;
    property Padding;
    property ParentBiDiMode;
    property ParentBackground;
    property ParentColor;
    property ParentCtl3D;
    property ParentDoubleBuffered;
    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property ShowHint;
    property TabOrder;
    property TabStop;
    property Touch;
    property Visible;
   // property StyleElements;
    property OnAlignInsertBefore;
    property OnAlignPosition;
    property OnCanResize;
    property OnClick;
    property OnConstrainedResize;
    property OnContextPopup;
    property OnDockDrop;
    property OnDockOver;
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnEndDock;
    property OnEndDrag;
    property OnEnter;
    property OnExit;
    property OnGesture;
    property OnGetSiteInfo;
    property OnMouseActivate;
    property OnMouseDown;
    property OnMouseEnter;
    property OnMouseLeave;
    property OnMouseMove;
    property OnMouseUp;
    property OnResize;
    property OnStartDock;
    property OnStartDrag;
    property OnUnDock;

    property SideWidth: ShortInt read FSideWidth write SetSideWidth;
    property MainPicture: TPicture read FMainPicture write SetMainPicture;
    property BackgroundPic: TPicture read FBackgroundPic write SetBackgroundPic;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('PPVCL', [TPPPanelTitle]);
end;

{ TPPPanelTitle }

constructor TPPPanelTitle.Create(AOwner: TComponent);
begin
  inherited;
  BevelOuter := bvNone;
  Align := alTop;
  Color := clHighlight;
  Height := 80;
  if AOwner is TCustomForm then
  begin
    FMainFrm := TCustomForm(AOwner);

  end;
  { FImage := TImage.Create(self);
    FImage.Parent := self;
    FImage.Stretch := True;
    FImage.Align := alClient;
    FImage.Transparent := True;

    FImage.OnClick := OnImgClick;
    FImage.OnDblClick := OnImgDblClick; }
  FMainPicture := TPicture.Create;
  FBackgroundPic := TPicture.Create;
end;

procedure TPPPanelTitle.DblClick;
var
  abd: TAppBarData; // Winapi.ShellAPI
begin
  if Assigned(FMainFrm) then
  begin
    { if FMainFrm.WindowState = wsNormal then
      FMainFrm.WindowState := wsMaximized
      else
      FMainFrm.WindowState := wsNormal; }
    if FMainFrm.WindowState = wsNormal then
    begin
      FMainFrm.WindowState := wsMaximized; // BorderStyleÎ为 bsNone
      abd.cbSize := sizeof(abd);
      SHAppBarMessage(ABM_GETTASKBARPOS, abd); // 读取任务栏的区域
      FMainFrm.Height := FMainFrm.Height - (abd.rc.Bottom - abd.rc.Top);
      // 预留出任务栏的位置
      // Screen.WorkAreaHeight
    end
    else if FMainFrm.WindowState = wsMaximized then
    begin
      FMainFrm.WindowState := wsNormal;
    end;
  end;
  inherited;
end;

destructor TPPPanelTitle.Destroy;
begin
  FBackgroundPic.Free;
  FMainPicture.Free;
  inherited Destroy;
end;

function TPPPanelTitle.GetTitleRect: TRect;
begin
  Result.Top := 5;
  Result.Left := FMainPicture.Graphic.Width + 10;//+ 其它按钮
  //if True then 如果居中 还要计算
  Result.Right := self.Width - 20;//要计算所有按钮的宽度
  Result.Bottom := 50;//这里要定义一个高度
end;

procedure TPPPanelTitle.MouseDown(Button: TMouseButton; Shift: TShiftState;
  X, Y: Integer);
begin
  inherited;
  if Assigned(FMainFrm) then
  begin
    ReleaseCapture;
    SendMessage(FMainFrm.Handle, WM_SYSCOMMAND, $F012, 0);
  end;
end;

procedure TPPPanelTitle.Paint;
var
  S: String;
  R: TRect;
begin
  inherited;
  //Canvas := Self.Canvas;
  R := GetClientRect;
  with Canvas do
  begin
    //画背景
    Brush.Color := Color;
    FillRect(R);
    //ICon
    if not Assigned(FMainPicture.Graphic) then
      FMainPicture.Icon.Assign(Application.Icon);
    Draw(5, 5, FMainPicture.Graphic);
    //Caption
    S := Caption;
    Font.Assign(Self.Font);
    R := GetTitleRect;
    if S <> '' then
      TextRect(R, S, [tfEndEllipsis, tfNoClip]);
  end;
  {
  with Canvas do
  begin
    Pen.Width := 1;
    Pen.Color := clBtnShadow;
  end;  }

end;

procedure TPPPanelTitle.SetBackgroundPic(const Value: TPicture);
begin
  FBackgroundPic.Assign(Value);
end;

procedure TPPPanelTitle.SetMainPicture(const Value: TPicture);
begin
  FMainPicture.Assign(Value);
end;

procedure TPPPanelTitle.SetSideWidth(const Value: ShortInt);
begin
  if FSideWidth = Value then
    Exit;

  FSideWidth := Value;
  if Assigned(FMainFrm) then
  begin
    Padding.Left := Value;
    Padding.Bottom := Value;
    Padding.Right := Value;
  end;
end;

end.
